package com.color.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.color.dao.IServiceNoDao;
import com.color.entity.ServiceNo;
import com.color.utils.ObjectUtils;

@Repository("serviceNoDao")
public class ServiceNoDaoImpl extends HibernateBaseDao<ServiceNo> implements IServiceNoDao{
	
	/**
	 * 新增或者修改
	 * @param ser
	 * @return
	 */
	public int Save(ServiceNo ser){
		return save(ser);
	}
	
	/**
	 * 新增、修改
	 */
	public void Save(List<ServiceNo> sers) {
		if(sers != null){
			for(ServiceNo ser : sers){
				save(ser);
			}
		}
	}
	
	/**
	 * 删除
	 * @param ser
	 * @return
	 */
	public int Delete(ServiceNo ser){
		return delete(ser);
	}
	
	/**
	 * 删除
	 */
	public void Delete(List<ServiceNo> sers) {
		if(sers != null){
			for(ServiceNo ser : sers){
				delete(ser);
			}
		}
	}
	
	/**
	 * 获取分页记录HQL
	 * @param page
	 * @param size
	 * @return
	 */
	public List<ServiceNo> getPageList(int page,int size){
		
		StringBuffer strHql = new StringBuffer();
		strHql.append(" from ServiceNo order by sNo desc");
		List<ServiceNo> list = openHqlPage(strHql.toString(),page,size);
		return list;
	}
	
	/**
	 * 获取分页记录SQL
	 */
	public List<ServiceNo> getPageListBySql(int page,int size){
		StringBuffer strSql = new StringBuffer();
		
		//Map方式传值
		/*strSql.append("select s_id,s_no from service_no order by s_id desc limit :start,:end ");
		
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("start", (page - 1) * size);
		map.put("end", page * size);
		
		Object objs = openSql(strSql.toString(),map);*/
		
		// 问号 占位符方式传值
		strSql.append("select s_id,s_no from service_no order by s_id desc limit ?,? ");
		int start = (page - 1) * size;
		int end = page * size;
		
		Object objs = openSql(strSql.toString(),new Object[]{start,end});
		
		//SQL查询结果导致泛型异常 故需要对结果集进行重新封装
		return convertEntity(objs);
	}
	
	/**
	 * 获取记录总数
	 */
	public int getCountList() {
		
		StringBuffer strHql = new StringBuffer();
		strHql.append(" select count(sId) as count from ServiceNo ");
		return openCountByHql(strHql.toString());
	}
	
	/**
	 * 根据期号获取
	 */
	public ServiceNo getOpenBysNo(String sNo) {
		StringBuffer strHql = new StringBuffer();
		strHql.append(" from ServiceNo where sNo = ? ");
		return openHqlOne(strHql.toString(),new Object[]{sNo});
	}
	
	/**
	 * 转换类
	 * @param objList
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<ServiceNo> convertEntity(Object objs){
		
		List<ServiceNo> list = new ArrayList<ServiceNo>();
		if(objs != null){
			List<Object> oList = (ArrayList<Object>) objs;
			if(oList.size() > 0){
				
				for(int i=0;i<oList.size();i++){
					Object[] obj = (Object[]) oList.get(i);
					ServiceNo ser = new ServiceNo();
					ser.setsId(ObjectUtils.convertInt(obj[0]));
					ser.setsNo(ObjectUtils.convertString(obj[1]));
					list.add(ser);
				}
			}
			
		}
		return list;
	}
	
}
